<?php 
include_once "../../Lib/DBMySql.inc";
include_once "../../Lib/Common.php";
include_once "../../Entities/webpagesObj.php";
include_once "../../DAL/webpagesDAL.php";
include_once "../../Lib/Content.php";
include_once "../../Lib/Session.php";
include_once "../header.php";

try
{
	$mainDB = NewDB(DATABASE_HOST, DATABASE_NAME , DATABASE_USER, DATABASE_PASSWORD);
	if (!isset($txtID) || $txtID=="")	$title		= "Add New Web Site";
	else	$title		= "Edit Web Site Information";
	$pageBack = "list.php";
	$frmName = "frmMain";
	$pageError	= "";
	
	$uploadPath = WS_ROOT."uploadfiles/";
	
	if (isset($act) && $act=="DELETE" && isset($deletedFile) && $deletedFile != "") {
		unlink($uploadPath.$deletedFile);
		$webpages = webpagesDAL::Load($mainDB, $txtID, $pageError);
		
		webpagesDAL::Update($mainDB, $webpages, $pageError);
	}

	if (isset($act) && ($act=="ADD" ||$act=="EDIT")) {
		if ($act=="EDIT") $editFilter = "AND id<>$txtID";
		$exist = webpagesDAL::IsExist($mainDB,  webpagesObj::nameFieldName()."='".mysql_escape_string($name)."' $editFilter");
	if ($exist && $pageError=="")
		$pageError = "Web site name is duplicated.";
	
	}
	if ($pageError=="")
	{
		
		if (isset($act) && ($act=="ADD" ||$act=="EDIT")) {
			if ($act=="ADD") {
				$webpages = new webpagesObj();
				webpagesDAL::Pop($webpages);
				
				
				
				if (!webpagesDAL::Insert($mainDB, $webpages, $error)) 
					$pageError = "Invalid inserting. ($error).";
				else 
					$exit = true;
			} else if ($act=="EDIT") {
				$webpages = new webpagesObj();
				$webpages = webpagesDAL::Load($mainDB, $txtID, $pageError);
				
				webpagesDAL::Pop($webpages);
				
				
				
				$webpages->id = $txtID;
				if (!webpagesDAL::Update($mainDB, $webpages, $error)) 
					$pageError = "Invalid updating. ($error).";
				else 
					$exit = true;
			}
		}
	}
	if (isset($txtID) && $txtID!="" && !isset($name)) {
		$webpages = webpagesDAL::Load($mainDB, $txtID, $pageError);
		if ($pageError=="" && $webpages!=null) {
			webpagesDAL::Push($webpages);
		}
	}
} 
catch(Exception $ex)
{ $pageError = $ex.message;}

?>
<form name=<?php  echo $frmName;?> method=post action="<?php  echo $_SERVER['PHP_SELF']; ?>" enctype="multipart/form-data">
<input type=hidden name=page value="<?php echo $page;?>">
<input type=hidden name=act value="<?php  if (!isset($txtID) || $txtID=="") echo "ADD"; else echo "EDIT";?>">
<input type=hidden name=txtID value="<?php  echo $txtID;?>">
<input type=hidden name=SortField value="<?php echo $SortField;?>">
<input type=hidden name=SortType value="<?php echo $SortType;?>">
<input type=hidden name="deletedFile">
<script>
<?php  if (isset($exit)) echo "document.$frmName.action = '$pageBack'; document.$frmName.submit();";?>
function ValidSubmit() {
	frm = document.<?php  echo $frmName;?>;
	
	if (!_CF_hasValue(frm.name, "TEXT")) {
		alert("Please enter web site name");
		frm.name.focus();
		return false;
	}

	/*
	if (!_CF_hasValue(frm.Email, "TEXT")) {
		alert("Please enter your email address.");
		frm.Email.focus();
		return false;
	}
	else if (!isEmail(frm.Email.value)) {
		alert("Email address is invalid.");
		frm.Email.focus();
		return false;
	}
	// Check number field
	if (_CF_hasValue(frm.NumberField, "TEXT") && !_CF_checknumber(frm.NumberField.value)) {
		alert("Invalid date, you must input a numeric data.");
		frm.NumberField.focus();
		return false;
	}
	if (_CF_hasValue(frm.NumberField, "TEXT") && !_CF_checkinteger(frm.NumberField.value)) {
		alert("Invalid date, you must input a integer number data.");
		frm.NumberField.focus();
		return false;
	}
	*/
	return true;		
}
</script>
<table width="97%" border="0" cellspacing="0" cellpadding="0" align="center">
	<tr valign=middle> 
		<td class="titleText" colspan=3 height=25><?php  echo $title;?></td>
	</tr>
	<tr> 
		<td align="center" valign="top" colspan=3>
			<table width="100%" border=1 cellspacing="1" class="formBackGround">
				<tr>
					<td>
						<TABLE border=0 class="gridBody" cellPadding=0 cellspacing="0" width="100%">
							<tr class="gridHeader">
								<td colspan=4 height=20 class='errortext'>&nbsp;<? echo $pageError;?>&nbsp;</td>
							</TR>
							<tr height=5>
								<td colspan=4 height=5></td>
							</TR>
							
							<tr class="gridBody" valign='top'>
								<td width="10" nowrap>&nbsp;</td>
								<td width="20%" nowrap>Web site name<font color="#cc0000">*</font>:&nbsp;</td>
								<td width="10" nowrap>&nbsp;</td>
								<td width="80%" nowrap><input type=text name="name" value="<?php  echo $name;?>" size="74" maxlength="255" class="inputText">&nbsp;</td>
							</TR>

							<tr class="gridBody" valign='top'>
								<td width="10" nowrap>&nbsp;</td>
								<td width="20%" nowrap>Description<font color="#cc0000"></font>:&nbsp;</td>
								<td width="10" nowrap>&nbsp;</td>
								<td width="80%" nowrap><textarea name="description" class="inputText" rows='5' cols='70'><?=$description;?></textarea>&nbsp;</td>
							</TR>

							<?php  
								if (!isset($txtID) || $txtID=="")
									include "../../Lib/bottomNewAct.php";
								else 
									include "../../Lib/bottomUpdateAct.php";
							?>
							<tr class="gridFooter" height=20>
								<td colspan=4></td>
							</TR>
						</TABLE>
					</td>
				</tr>
			</TABLE>
		</td>
	</tr>
</table>
</form>
<script>document.<?=$frmName;?>.name.focus();</script>
<?php 
include "../footer.php";
?>